package com.example.myspringboot.kafkaUnits;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.myspringboot.domain.ImChatUserGroupMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author zheng
 */
@Controller
public class KafkaTestController {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @RequestMapping("/testKafka")
    public ResponseEntity sendMessage() {
        ImChatUserGroupMessage imChatUserGroupMessage = new ImChatUserGroupMessage();
        imChatUserGroupMessage.setContent("testKafka~");
        imChatUserGroupMessage.setId(1L);
        kafkaTemplate.send("testDemo", JSONObject.toJSONString(imChatUserGroupMessage));
        return ResponseEntity.ok("发送成功");
    }

    @RequestMapping("/testKafkaZhengshan")
    public ResponseEntity sendZhengshanMessage() {
        ImChatUserGroupMessage imChatUserGroupMessage = new ImChatUserGroupMessage();
        imChatUserGroupMessage.setContent("zhengshan~");
        imChatUserGroupMessage.setId(1L);
        kafkaTemplate.send("zhengshanTest", JSONObject.toJSONString(imChatUserGroupMessage));
        return ResponseEntity.ok("发送成功");
    }
}
